package vn.personal.anhht.bloback12.database;




import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CallBlockDatabaseSMSAdapter {
	
	
	public static final String KEY_ID = "id";
	public static final String KEY_TIME = "receive_time";
	public static final String KEY_FROM = "phone_from";
	public static final String KEY_CONTENT = "content";
	public static final String KEY_TYPE = "type";
	
	public static final String TABLE_SMS = "smss";
	
	
	private Context context;
	private SQLiteDatabase database;
	private CallBlockDatabaseHelper dbHelper;
	
	
	public CallBlockDatabaseSMSAdapter(){
		this.context = null;
	}
	
	public CallBlockDatabaseSMSAdapter(Context _context){
		this.context = _context;
	}
	
	public CallBlockDatabaseSMSAdapter open() throws SQLException {
		dbHelper = new CallBlockDatabaseHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		dbHelper.close();
	}

	public android.database.Cursor selectAllSms(){
		String[] columns = {KEY_ID, KEY_FROM, KEY_TIME, KEY_CONTENT};
		String selection = null;
		String[] selectionAgrs = null;
		String groupBy = null;
		String having = null;
		String orderBy = KEY_TIME + " DESC";
		
		return this.database.query(TABLE_SMS, columns, selection, selectionAgrs, groupBy, having, orderBy);
	}

	public android.database.Cursor selectSmsById(int _id){
		String[] columns = {KEY_ID, KEY_FROM, KEY_TIME, KEY_CONTENT};
		String selection = KEY_ID + " = " + _id;
		String[] selectionAgrs = null;
		String groupBy = null;
		String having = null;
		String orderBy = null;
		
		return this.database.query(TABLE_SMS, columns, selection, selectionAgrs, groupBy, having, orderBy);
	}
	
	public android.database.Cursor selectSmsByPhone(String phone){
		String[] columns = {KEY_ID, KEY_FROM, KEY_TIME, KEY_CONTENT};
		String selection = KEY_FROM + " = '" + phone + "'";
		String[] selectionAgrs = null;
		String groupBy = null;
		String having = null;
		String orderBy = null;
		return this.database.query(TABLE_SMS, columns, selection, selectionAgrs, groupBy, having, orderBy);
	}
	
	private ContentValues initParameters(
			String from, 
			String time, 
			String content, 
			String type){
		ContentValues parameters = new ContentValues();	
		parameters.put(KEY_FROM, from);
		parameters.put(KEY_TIME, time);
		parameters.put(KEY_CONTENT, content);
		parameters.put(KEY_TYPE, "");
		return parameters;	
	}

	public long addSms(
			String from, 
			String time, 
			String content, 
			String type){
		ContentValues parameters = initParameters(from, time, content, type);
		return this.database.insert(TABLE_SMS, null, parameters);
	}

	public boolean updateSms(
			int id,
			String from, 
			String time, 
			String content, 
			String type){
		ContentValues parameters = initParameters(from, time, content, type);
		//update function return a number of row affected
		return this.database.update(TABLE_SMS, parameters, KEY_ID + " = " + id, null) > 0;
	}
	
	public boolean updateAdvanced(ContentValues parameters, String whereClause, String[] whereArgs){
		
		return this.database.update(TABLE_SMS, parameters, whereClause, whereArgs) > 0;
	}

	public boolean deleteSms(int _id){
		return this.database.delete(TABLE_SMS, KEY_ID + " = " + _id, null) > 0;
	}
	
	public boolean deleteAll(){
		return this.database.delete(TABLE_SMS, null , null) > 0;
	}
	
}